package com.example.coroutinedemo

import android.app.Activity
import android.app.ProgressDialog
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import kotlinx.coroutines.Job
import kotlinx.coroutines.newFixedThreadPoolContext


class LoadingImageActivity : AppCompatActivity() {

    companion object {
        private const val IMAGE_PICK_REQUEST = 101
    }

    private var imageUri: Uri? = null
    internal val Background = newFixedThreadPoolContext(2, "bg")
    private var dialog: ProgressDialog? = null;
    private var job: Job? = null;

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_image)
        val intent = Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI).also { it.type = "image/*" }
        startActivityForResult(intent, IMAGE_PICK_REQUEST)
        dialog = ProgressDialog(this)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == IMAGE_PICK_REQUEST && resultCode == Activity.RESULT_OK && data != null) {
            imageUri = data.data // So we can store it in onSaveInstanceState
            dialog?.setMessage("正在加载图片...")
//            dialog?.setCancelable(false)
            dialog?.show()
//            job = async(Background) {
//                //模拟长耗时
//                Log.i("1当前线程名字", Thread.currentThread().name)
//                delay(6000)
//                var bitmap: Bitmap = MediaStore.Images.Media.getBitmap(contentResolver, imageUri)
//
//                launch(UI) {
//                    image_view.setImageBitmap(bitmap)
//                    Log.i("2当前线程名字", Thread.currentThread().name)
//                    dialog?.dismiss()
//                }
//            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        job?.cancel()
    }


}
